<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2017/11/20
 * Time: 下午5:26
 */

namespace App\Console\Commands;

use App\Modules\Agent\Services\AgentServices;
use App\Modules\BackCost\Models\HbGzh;
use App\Modules\BackCost\Models\PromotionGroupsSdModel;
use App\Modules\BackCost\Services\HbGzhsServices;
use App\Modules\BackCost\Models\HbMonthStats;
use App\Modules\BackCost\Services\HbMonthStatsServices;
use App\Modules\Channel\Services\ChannelDailyStatService;
use App\Modules\SendOrder\Models\SendOrder;
use App\Modules\SendOrder\Services\SendOrderService;
use App\Modules\Trade\Services\OrderService;
use App\Modules\User\Models\UserSubscribeBehaviorStats;
use DB;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Log;

class SendOrderModel extends Command
{
    /**
     * 执行命令 获取站点的派单数据及服务号信息
     *
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'generate_sd_month_stats';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '派单回本模型';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //30天
        for($i=17;$i>=2;$i--){
            $start_date = date('Y-m-01',strtotime('-'.$i.' month'));
            $end_date = date('Y-m-d',strtotime(date('Y-m-01',strtotime('-'.($i-1).' month')).' -1 day'));

            $recharge = DB::connection('statistic')
                ->table('send_orders')
                ->join('orders','orders.send_order_id','=','send_orders.id')
                ->where('orders.status','PAID')
                ->whereBetween('send_orders.pre_send_date',[$start_date,$end_date])
                ->whereRaw('UNIX_TIMESTAMP(orders.created_at)-UNIX_TIMESTAMP(send_orders.pre_send_date) <= 2592000')
                ->sum('orders.price');
            $cost = DB::connection('statistic')
                ->table('send_orders')
                ->whereBetween('send_orders.pre_send_date',[$start_date,$end_date])
                ->sum('cost');
            $to_insert = ['cost'=>$cost,'thirty_days'=>$recharge];
            $index = ['month'=>$start_date];
            PromotionGroupsSdModel::updateOrCreate($index,$to_insert);
        }


        $promotionStats = PromotionGroupsSdModel::orderBy('id','desc')->get();
        $this_month = date('Y-m-02');
        foreach ($promotionStats as $promotionStat) {
            $this_month_created = date_create($this_month);
            $p_month = date_create($promotionStat->month);
            $interval = date_diff($this_month_created,$p_month);
            $month_diff = $interval->format('%m');
            //60tian
            if($month_diff>=4 && empty($promotionStat->sixty_days)) {
                $sixty_recharge = DB::connection('statistic')
                    ->table('send_orders')
                    ->join('orders','orders.send_order_id','=','send_orders.id')
                    ->where('orders.status','PAID')
                    ->whereBetween('send_orders.pre_send_date',[$promotionStat->month,date('Y-m-d',strtotime(($promotionStat->month).' +1 month -1 day'))])
                    ->whereRaw('UNIX_TIMESTAMP(orders.created_at)-UNIX_TIMESTAMP(send_orders.pre_send_date) <= 5184000')
                    ->sum('orders.price');
                $promotionStat->sixty_days = $sixty_recharge;
                $promotionStat->save();
            }
            //90tian
            if($month_diff>=5 && empty($promotionStat->ninty_days)) {
                $ninty_days = DB::connection('statistic')
                    ->table('send_orders')
                    ->join('orders','orders.send_order_id','=','send_orders.id')
                    ->where('orders.status','PAID')
                    ->whereBetween('send_orders.pre_send_date',[$promotionStat->month,date('Y-m-d',strtotime(($promotionStat->month).' +1 month -1 day'))])
                    ->whereRaw('UNIX_TIMESTAMP(orders.created_at)-UNIX_TIMESTAMP(send_orders.pre_send_date) <= 7776000')
                    ->sum('orders.price');
                $promotionStat->ninty_days = $ninty_days;
                $promotionStat->save();
            }
            //120tian
            if($month_diff>=6 && empty($promotionStat->one_hundred_twity)) {
                $one_hundred_twity = DB::connection('statistic')
                    ->table('send_orders')
                    ->join('orders','orders.send_order_id','=','send_orders.id')
                    ->where('orders.status','PAID')
                    ->whereBetween('send_orders.pre_send_date',[$promotionStat->month,date('Y-m-d',strtotime(($promotionStat->month).' +1 month -1 day'))])
                    ->whereRaw('UNIX_TIMESTAMP(orders.created_at)-UNIX_TIMESTAMP(send_orders.pre_send_date) <= 10368000')
                    ->sum('orders.price');
                $promotionStat->one_hundred_twity = $one_hundred_twity;
                $promotionStat->save();
            }
            //150tian
            if($month_diff>=7 && empty($promotionStat->one_hundred_fifty)) {
                $one_hundred_fifty = DB::connection('statistic')
                    ->table('send_orders')
                    ->join('orders','orders.send_order_id','=','send_orders.id')
                    ->where('orders.status','PAID')
                    ->whereBetween('send_orders.pre_send_date',[$promotionStat->month,date('Y-m-d',strtotime(($promotionStat->month).' +1 month -1 day'))])
                    ->whereRaw('UNIX_TIMESTAMP(orders.created_at)-UNIX_TIMESTAMP(send_orders.pre_send_date) <= 12960000')
                    ->sum('orders.price');
                $promotionStat->one_hundred_fifty = $one_hundred_fifty;
                $promotionStat->save();
            }
            //180tian
            if($month_diff>=8 && empty($promotionStat->one_hundred_eighty)) {
                $one_hundred_eighty = DB::connection('statistic')
                    ->table('send_orders')
                    ->join('orders','orders.send_order_id','=','send_orders.id')
                    ->where('orders.status','PAID')
                    ->whereBetween('send_orders.pre_send_date',[$promotionStat->month,date('Y-m-d',strtotime(($promotionStat->month).' +1 month -1 day'))])
                    ->whereRaw('UNIX_TIMESTAMP(orders.created_at)-UNIX_TIMESTAMP(send_orders.pre_send_date) <= 15552000')
                    ->sum('orders.price');
                $promotionStat->one_hundred_eighty = $one_hundred_eighty;
                $promotionStat->save();
            }
            //210tian
            if($month_diff>=9 && empty($promotionStat->two_hundred_ten)) {
                $two_hundred_ten = DB::connection('statistic')
                    ->table('send_orders')
                    ->join('orders','orders.send_order_id','=','send_orders.id')
                    ->where('orders.status','PAID')
                    ->whereBetween('send_orders.pre_send_date',[$promotionStat->month,date('Y-m-d',strtotime(($promotionStat->month).' +1 month -1 day'))])
                    ->whereRaw('UNIX_TIMESTAMP(orders.created_at)-UNIX_TIMESTAMP(send_orders.pre_send_date) <= 18144000')
                    ->sum('orders.price');
                $promotionStat->two_hundred_ten = $two_hundred_ten;
                $promotionStat->save();
            }
            //240tian
            if($month_diff>=10 && empty($promotionStat->two_hundred_fourty)) {
                $two_hundred_fourty = DB::connection('statistic')
                    ->table('send_orders')
                    ->join('orders','orders.send_order_id','=','send_orders.id')
                    ->where('orders.status','PAID')
                    ->whereBetween('send_orders.pre_send_date',[$promotionStat->month,date('Y-m-d',strtotime(($promotionStat->month).' +1 month -1 day'))])
                    ->whereRaw('UNIX_TIMESTAMP(orders.created_at)-UNIX_TIMESTAMP(send_orders.pre_send_date) <= 20736000')
                    ->sum('orders.price');
                $promotionStat->two_hundred_fourty = $two_hundred_fourty;
                $promotionStat->save();
            }
            //270tian
            if($month_diff>=11 && empty($promotionStat->two_hundred_seventy)) {
                $two_hundred_seventy = DB::connection('statistic')
                    ->table('send_orders')
                    ->join('orders','orders.send_order_id','=','send_orders.id')
                    ->where('orders.status','PAID')
                    ->whereBetween('send_orders.pre_send_date',[$promotionStat->month,date('Y-m-d',strtotime(($promotionStat->month).' +1 month -1 day'))])
                    ->whereRaw('UNIX_TIMESTAMP(orders.created_at)-UNIX_TIMESTAMP(send_orders.pre_send_date) <= 23328000')
                    ->sum('orders.price');
                $promotionStat->two_hundred_seventy = $two_hundred_seventy;
                $promotionStat->save();
            }
            //300tian
            if($month_diff>=12 && empty($promotionStat->three_hundred)) {
                $three_hundred = DB::connection('statistic')
                    ->table('send_orders')
                    ->join('orders','orders.send_order_id','=','send_orders.id')
                    ->where('orders.status','PAID')
                    ->whereBetween('send_orders.pre_send_date',[$promotionStat->month,date('Y-m-d',strtotime(($promotionStat->month).' +1 month -1 day'))])
                    ->whereRaw('UNIX_TIMESTAMP(orders.created_at)-UNIX_TIMESTAMP(send_orders.pre_send_date) <= 25920000')
                    ->sum('orders.price');
                $promotionStat->three_hundred = $three_hundred;
                $promotionStat->save();
            }
            //330tian
            if($month_diff>=13 && empty($promotionStat->three_hundred_thirty)) {
                $three_hundred_thirty = DB::connection('statistic')
                    ->table('send_orders')
                    ->join('orders','orders.send_order_id','=','send_orders.id')
                    ->where('orders.status','PAID')
                    ->whereBetween('send_orders.pre_send_date',[$promotionStat->month,date('Y-m-d',strtotime(($promotionStat->month).' +1 month -1 day'))])
                    ->whereRaw('UNIX_TIMESTAMP(orders.created_at)-UNIX_TIMESTAMP(send_orders.pre_send_date) <= 28512000')
                    ->sum('orders.price');
                $promotionStat->three_hundred_thirty = $three_hundred_thirty;
                $promotionStat->save();
            }
            //360tian
            if($month_diff>=14 && empty($promotionStat->three_hundred_sixty)) {
                $three_hundred_sixty = DB::connection('statistic')
                    ->table('send_orders')
                    ->join('orders','orders.send_order_id','=','send_orders.id')
                    ->where('orders.status','PAID')
                    ->whereBetween('send_orders.pre_send_date',[$promotionStat->month,date('Y-m-d',strtotime(($promotionStat->month).' +1 month -1 day'))])
                    ->whereRaw('UNIX_TIMESTAMP(orders.created_at)-UNIX_TIMESTAMP(send_orders.pre_send_date) <= 31104000')
                    ->sum('orders.price');
                $promotionStat->three_hundred_sixty = $three_hundred_sixty;
                $promotionStat->save();
            }


        }

    }

    private static function getOfficialAccountFansNum($appid,$start_date,$end_date){
        $client = New Client(['base_uri' => env('MEDIA_API_BASE_URI')]);
        $response = $client->request('GET','get_gzh_statistics',['query'=>['gzh_app_id'=>$appid,'from_date'=>$start_date,'to_date'=>$end_date]]);
        $res = json_decode($response->getBody(),true);
        //if(isset($res['']))
        return $res['data'];
    }
}